package cn.wh.pay.factory.defaults;

import cn.wh.pay.factory.PayFactory;
import cn.wh.pay.type.ltzf.config.LtzfConfiguration;
import cn.wh.pay.type.ltzf.payments.app.AppPayService;
import cn.wh.pay.type.ltzf.payments.app.IAppPayApi;
import cn.wh.pay.type.ltzf.payments.h5.H5PayService;
import cn.wh.pay.type.ltzf.payments.h5.IH5PayApi;
import cn.wh.pay.type.ltzf.payments.jsapi.IJSPayApi;
import cn.wh.pay.type.ltzf.payments.jsapi.JSPayService;
import cn.wh.pay.type.ltzf.payments.jump_h5.IJumpH5PayApi;
import cn.wh.pay.type.ltzf.payments.jump_h5.JumpH5PayService;
import cn.wh.pay.type.ltzf.payments.nativepay.INativePayApi;
import cn.wh.pay.type.ltzf.payments.nativepay.NativePayService;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Author wh
 * @Description
 * @Date 2024/4/25 14:02
 * @Version 1.0
 **/
public abstract class LtzfPayAbstarctFactory implements PayFactory {
    private NativePayService nativePayService;

    private AppPayService appPayService;

    private H5PayService h5PayService;

    private JumpH5PayService jumpH5PayService;

    private JSPayService jsPayService;

    private final LtzfConfiguration configuration;

    private final OkHttpClient httpClient;

    public LtzfPayAbstarctFactory(LtzfConfiguration configuration) {
        this.configuration = configuration;
        // 1. 日志配置
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(configuration.getLevel());

        // 2. 开启 HTTP 客户端
        this.httpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .connectTimeout(configuration.getConnectTimeout(), TimeUnit.SECONDS)
                .writeTimeout(configuration.getWriteTimeout(), TimeUnit.SECONDS)
                .readTimeout(configuration.getReadTimeout(), TimeUnit.SECONDS)
                .build();

    }

    @Override
    public  NativePayService nativePayService() {
        if (nativePayService == null) {
            // 构建API
            INativePayApi nativePayApi = new Retrofit.Builder()
                    .baseUrl(configuration.getApiHost())
                    .client(httpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build()
                    .create(INativePayApi.class);
            nativePayService = new NativePayService(configuration, nativePayApi);
        }
        // 创建Native支付服务
        return nativePayService;
    }

    @Override
    public H5PayService h5PayService() {
        if (h5PayService == null) {
            // 构建API
            IH5PayApi h5PayApi = new Retrofit.Builder()
                    .baseUrl(configuration.getApiHost())
                    .client(httpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build()
                    .create(IH5PayApi.class);
            // 创建支付服务
            h5PayService = new H5PayService(configuration, h5PayApi);
        }
        // 创建支付服务
        return h5PayService;
    }

    @Override
    public AppPayService appPayService() {
        if (appPayService == null) {
            // 构建API
            IAppPayApi appPayApi = new Retrofit.Builder()
                    .baseUrl(configuration.getApiHost())
                    .client(httpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build()
                    .create(IAppPayApi.class);
            // 创建支付服务
            appPayService = new AppPayService(configuration, appPayApi);
        }
        return appPayService;
    }

    @Override
    public JSPayService jsPayService() {
        if (jsPayService == null) {
            // 构建API
            IJSPayApi jsPayApi = new Retrofit.Builder()
                    .baseUrl(configuration.getApiHost())
                    .client(httpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build()
                    .create(IJSPayApi.class);
            // 创建支付服务
            jsPayService = new JSPayService(configuration, jsPayApi);
        }
        return jsPayService;
    }

    @Override
    public JumpH5PayService jumpH5PayService() {
        if (jumpH5PayService == null) {
            // 构建API
            IJumpH5PayApi jumpH5PayApi = new Retrofit.Builder()
                    .baseUrl(configuration.getApiHost())
                    .client(httpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(JacksonConverterFactory.create())
                    .build()
                    .create(IJumpH5PayApi.class);
            // 创建支付服务
            jumpH5PayService = new JumpH5PayService(configuration, jumpH5PayApi);
        }
        return jumpH5PayService ;
    }

    @Override
    public OkHttpClient getOkHttpClient() {
        return this.httpClient;
    }


}
